import { Provide, Inject } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { WorkingRecordsModel } from '../entity/working_records.methods';
import { AppConfigService } from './app.config.service';
@Provide()
//  工时登记
export class WorkingDepartService {
    @Inject()
    ctx: Context;
    @Inject()
    WorkingRecordsModel: WorkingRecordsModel;
    @Inject()
    AppConfigService: AppConfigService;
    /**
     * 获取用户所有工时数据
     * @param {*} unionId 
     */
    async getWorkingDataByUserId(params: any): Promise<any> {
        const { unionId } = params;
        return await this.WorkingRecordsModel.getWorkingDataByUserId(unionId);
    }

    /**
     * 查询指定部门下所有用户汇总工时信息
     * @param {*} deptId 
     * @returns 
     */
    async getAllWorkingPageByDeptId(params: any): Promise<any> {
        const { deptId } = params;
        //  获取当前部门的所有用户id
        const userIds = await this.getDeptUserId(deptId);
        if (!userIds || !userIds.length) {
            return [];
        }
        // 获取部门下所有用户信息
        return await this.WorkingRecordsModel.getAllDataByDeptId(userIds);
    }

    /**
     * 根据部门id查询部门用户id
     * @param deptId 
     * @returns 
     */
    async getDeptUserId(deptId: any): Promise<any> {
        const deptLists: any = await this.AppConfigService.getDataByCode("DeptsUserTree");
        if (!deptLists || !deptLists['DeptsUserTree'].dept_lists) return [];

        //  获取当前选中的部门信息
        const tagDept = await this.pollingDeptData(deptLists['DeptsUserTree'].dept_lists, deptId);
        //  获取当前部门的用户id
        const ids: [] = await this.pollingUserId(tagDept.dept_lists, tagDept.user_lists);
        //  用户id去重
        return ids.filter(function (value, index, self) {
            return self.indexOf(value) === index;
        });
    }

    /**
     * 轮询调用获取用户当前选中的部门数据
     * @param {*} deptLists 
     * @param {*} appToken 
     * @returns 
     */
    async pollingDeptData(deptLists: any, tagId: number): Promise<any> {
        try {
            let obj = null;
            for (let i = 0; i < deptLists.length; i++) {
                if (deptLists[i].dept_id == tagId) {
                    obj = deptLists[i];
                } else if (deptLists[i].dept_lists && deptLists[i].dept_lists.length) {
                    const data = await this.pollingDeptData(deptLists[i].dept_lists, tagId);
                    if (data) {
                        obj = data;
                    }
                }
            }
            return obj;
        } catch (error) {
            console.log("######  pollingDeptData-error:", error);
            return {};
        }
    }

    /**
     * 轮询调用获取当前部门的用户列表
     * @param {*} deptLists 
     * @param {*} userLists 
     * @returns 
     */
    async pollingUserId(deptLists: any, userLists: any): Promise<any> {
        try {
            let _ids = [];
            userLists.forEach(item => {
                _ids.push(item.userid)
            })
            for (let i = 0; i < deptLists.length; i++) {
                const data = await this.pollingUserId(deptLists[i].dept_lists, deptLists[i].user_lists);
                _ids = _ids.concat(data);
            }
            return _ids;
        } catch (error) {
            return {};
        }
    }

    /**
     * 工时汇总图表数据获取
     * @param {*} startTime 
     * @param {*} endTime 
     * @param {*} type 
     * @param {*} deptId 
     * @returns 
     */
    async getChartsData(params: any): Promise<any> {
        const { startTime, endTime, type, deptId } = params;
        if (type === 'user') {
            //  根据用户查询
            if (deptId) {
                //  查询当前部门所有用户信息
                const userIds = await this.getDeptUserId(deptId);
                if (!userIds || !userIds.length) return [];
                return await this.WorkingRecordsModel.getDataByDeptIdAndUser(startTime, endTime, userIds);
            } else {
                return await this.WorkingRecordsModel.getDataByUser(startTime, endTime);
            }
        } else {
            //  根据项目查询
            if(deptId){
                //  查询当前部门所有用户信息
                const userIds = await this.getDeptUserId(deptId);
                if (!userIds || !userIds.length) return [];
                return await this.WorkingRecordsModel.getDataByDeptIdAndProj(startTime, endTime, userIds)
            }else{
                return await this.WorkingRecordsModel.getDataByProj(startTime, endTime);
            }
        }
    }

}